package com.taotao.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ItemService;
import com.taotao.utils.JsonUtils;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private TbItemMapper tbItemMapper;
@Autowired
private TbItemDescMapper tbItemDescMapper;
@Autowired
private TbItemParamItemMapper itemParamItemMapper;

@Autowired 
private JedisClient jedisClient;
@Value("${REDIS_ITEM_KEY}")
private String REDIS_ITEM_KEY;
@Value("${REDIS_ITEM_EXPIRE}")
private Integer REDIS_ITEM_EXPIRE;
	@Override
	/**
	 * 获取商品的基本信息
	 */
	public TaotaoResult getItemBaseInfo(long itemId) {
		//获取redis数据
		try{
			String json=jedisClient.get(REDIS_ITEM_KEY+":" + itemId + ":base");
			if(!StringUtils.isBlank(json)){
				TbItem item=JsonUtils.jsonToPojo(json, TbItem.class);
				return TaotaoResult.ok(item);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		TbItemExample example =new TbItemExample();
		Criteria criteria= example.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> list=tbItemMapper.selectByExample(example);
		TbItem item=list.size()>0?list.get(0):null;
		//存入redis
		try{
			jedisClient.set(REDIS_ITEM_KEY+":" + itemId + ":base",JsonUtils.objectToJson(item));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":base", REDIS_ITEM_EXPIRE);
		}catch(Exception e){
			e.printStackTrace();
		}
		return TaotaoResult.ok(item);
	}
	@Override
	/**
	 * 获取商品的详细信息
	 */
	public TaotaoResult getItemDesc(long itemId) {
		try{//取缓存中的数据
			String json=jedisClient.get(REDIS_ITEM_KEY+":" + itemId + ":desc");
			if(!StringUtils.isBlank(json)){
				TbItemDesc	itemDesc= JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return TaotaoResult.ok(itemDesc);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		TbItemDesc itemDesc =tbItemDescMapper.selectByPrimaryKey(itemId);
		try{//写入缓存
			jedisClient.set(REDIS_ITEM_KEY+":" + itemId + ":desc", JsonUtils.objectToJson(itemDesc));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":desc", REDIS_ITEM_EXPIRE);
		}catch(Exception e){
			e.printStackTrace();
		}
		return TaotaoResult.ok(itemDesc);
	}
	@Override
	public TaotaoResult getItemParam(long itemId) {
		//添加缓存
		try {
			//添加缓存逻辑
			//从缓存中取商品信息，商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":param");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItemParamItem paramItem = JsonUtils.jsonToPojo(json, TbItemParamItem.class);
				return TaotaoResult.ok(paramItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//根据商品id查询规格参数
		//设置查询条件
		TbItemParamItemExample example = new TbItemParamItemExample();
		example.createCriteria().andItemIdEqualTo(itemId);
		//执行查询
		List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(example);
		if (list != null && list.size()>0) {
			TbItemParamItem paramItem = list.get(0);
			try {
				//把商品信息写入缓存
				jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":param", JsonUtils.objectToJson(paramItem));
				//设置key的有效期
				jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":param", REDIS_ITEM_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return TaotaoResult.ok(paramItem);
		}
		return TaotaoResult.build(400, "无此商品规格");
	}


}
